package cn.bqjr.eily.mybatis.spring.boot.autoconfigure;

import com.github.pagehelper.PageInterceptor;
import com.github.pagehelper.autoconfigure.PageHelperProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableConfigurationProperties({PageHelperProperties.class})
public class MybatisAutoconfigure {
    private Logger logger = LoggerFactory.getLogger(MybatisAutoconfigure.class);

    /**
     * pageHelper分页插件
     *
     * @return
     */
    @Bean
    public PageInterceptor pageInterceptor(PageHelperProperties pageHelperProperties) {
        PageInterceptor interceptor = new PageInterceptor();
        interceptor.setProperties(pageHelperProperties.getProperties());
        logger.debug("PageInterceptor interceptor init successful");
        return interceptor;
    }

    /**
     * 配置事务管理器
     */
    @Bean
    @ConditionalOnMissingBean
    public DataSourceTransactionManager defaultTransactionManager(DataSource dataSource) throws Exception {
        return new DataSourceTransactionManager(dataSource);
    }
}